<?php

/*
 * Formulář pro vytvoření nové schránky, ze které se budou stahovat maily
 * Implmentace drupal formulářů
 *  - _form -formulář, _form_validate - validace formuláře před odesláním, _form_submit - odeslání formuláře
*/
function mailcontent_settings_add_mail_form($form_state) {

    $form['add'] = array(
            '#type' => 'fieldset',
            '#title' => t('Add new account'),
            '#collapsible' => TRUE,
            '#collapsed' => TRUE,
    );

    $form['add']['username'] = array(
            '#type' => 'textfield',
            '#title' => t('Username for mailbox'),
            '#default_value' => '');

    $form['add']['password'] = array(
            '#type' => 'password',
            '#title' => t('Password for mailbox'),
            '#default_value' => '');

    $form['add']['password_confirm'] = array(
            '#type' => 'password',
            '#title' => t('Repeat rassword for mailbox'),
            '#default_value' => '');

    $form['add']['server'] = array(
            '#type' => 'textfield',
            '#title' => t('Alternative mailserver, use form like "pop3.live.com:995/pop3/ssl"'),
            '#default_value' => '');

    $form['add']['timeout'] = array(
            '#type' => 'textfield',
            '#title' => t('Timeout for next connection (in minutes)'),
            '#default_value' => '0');

    $form['add']['button'] = array(
            '#type' => 'submit',
            '#value' => t('Save'));

    return $form;
} //OK
function mailcontent_settings_add_mail_form_validate($form, &$form_state) {
    if ($form_state['values']['username'] == '') {
        form_set_error('', t('Missing username in Add new account!!'));
    }
    if ($form_state['values']['password'] == '') {
        form_set_error('', t('Missing password in Add new account!!'));
    }
    if ($form_state['values']['password_confirm'] == '') {
        form_set_error('', t('Missing confirmation password in Add new account!!'));
    }
    if ($form_state['values']['password_confirm'] != $form_state['values']['password']) {
        form_set_error('', t('Passwords are not same in Add new account!!'));
    }
//TODO dodelat kontrolu timeout - jestli to je cislo
} //OK
function mailcontent_settings_add_mail_form_submit($form, &$form_state) {

    global $user;
    if (user_access('settings accounts')) {


        $write_to_db = mailcontent_db_login_add($form_state['values']['username'], $form_state['values']['password'], $form_state['values']['server'], $form_state['values']['timeout']);
        if ($write_to_db) {
            drupal_set_message(t('New account for mailbox created!'));
        } else {
            form_set_error(t('Fault - new account was not created!'));
        }
    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }

}//Ok

/*
 * Formulář pro editaci schránky, ze které se budou stahovat maily
 * Implmentace drupal formulářů
 *  - _form -formulář, _form_validate - validace formuláře před odesláním, _form_submit - odeslání formuláře
*/
function mailcontent_settings_edit_mail_form($form_state) {
    global $user;
    if (db_result(db_query("SELECT * FROM {mailcontent_login} WHERE duid=".$user->uid." and idacc=".$GLOBALS['mailcontent_idacc']))) {
        $result = db_query("SELECT * FROM {mailcontent_login} WHERE duid=".$user->uid." and idacc=".$GLOBALS['mailcontent_idacc']);
        $row = db_fetch_array($result);
    }
    $form['idacc'] = array(
            '#type' => 'hidden',
            '#title' => t('Old ID for record'),
            '#default_value' => $GLOBALS["mailcontent_idacc"],
    );
    $form['edit'] = array(
            '#type' => 'fieldset',
            '#title' => t('Change name for '.$row['username']),
            '#collapsible' => TRUE,
            '#collapsed' => FALSE,
    );
    $form['edit']['new_username'] = array(
            '#type' => 'textfield',
            '#title' => t('New username for mailbox'),
            '#default_value' => $row['username'],
    );
    $form['edit']['server'] = array(
            '#type' => 'textfield',
            '#title' => t('Alternative mailserver, use form like "pop3.live.com:995/pop3/ssl"'),
            '#default_value' => $row['server']
    );
    $form['edit']['timeout'] = array(
            '#type' => 'textfield',
            '#title' => t('Timeout for next connection (in minutes)'),
            '#default_value' => $row['timeout']
    );
    $form['edit']['button'] = array(
            '#type' => 'submit',
            '#value' => t('Save'),
    );
    $form['edit']['cancel'] = array(
            '#type' => 'submit',
            '#value' => t('Cancel'),
            '#submit' => array('mailcontent_goto_settings'),
    );
    return $form;
} //OK
function mailcontent_settings_edit_mail_form_validate($form, &$form_state) {
    if ($form_state['values']['new_username'] == '') {
        form_set_error('', t('New username is missing!!'));
    }
}//OK
function mailcontent_settings_edit_mail_form_submit($form, &$form_state) {
    global $user;
    if (user_access('settings accounts')) {


        global $user;
        if (db_query("UPDATE {mailcontent_login} SET username='".$form_state['values']['new_username']."', server='".$form_state['values']['server']."', timeout=".$form_state['values']['timeout']." WHERE duid=".$user->uid." and idacc=".$form_state['values']['idacc'])) {
            drupal_set_message(t('Login has been changed..'));
            drupal_goto('mailcontent/settings/accounts');
        } else {
            form_set_error('', t('Database fault'));
        }
    } else {
        $text = "<hr><br><br>".t("You have not permission for this action!");
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
}//OK

/*
 * Formulář pro změnu hesla ke schránce, ze které se budou stahovat maily
 * Implmentace drupal formulářů
 *  - _form -formulář, _form_validate - validace formuláře před odesláním, _form_submit - odeslání formuláře
*/
function mailcontent_settings_passwd_mail_form($form_state) {
    global $user;
    if (db_result(db_query("SELECT * FROM {mailcontent_login} WHERE duid=".$user->uid." and idacc=".$GLOBALS['mailcontent_idacc']))) {
        $result = db_query("SELECT * FROM {mailcontent_login} WHERE duid=".$user->uid." and idacc=".$GLOBALS['mailcontent_idacc']);
        $row = db_fetch_array($result);
    }
    $form['idacc'] = array(
            '#type' => 'hidden',
            '#title' => t('Login of account for change password'),
            '#default_value' => $GLOBALS['mailcontent_idacc']
    );

    $form['passwd'] = array(
            '#type' => 'fieldset',
            '#title' => t('Change password for '.$row['username']),
            '#collapsible' => TRUE,
            '#collapsed' => FALSE,
    );

    $form['passwd']['password_old'] = array(
            '#type' => 'password',
            '#title' => t('Old password for mailbox - account '.$row['username']),
            '#default_value' => '',
    );

    $form['passwd']['password'] = array(
            '#type' => 'password',
            '#title' => t('New password for mailbox - account '.$row['username']),
            '#default_value' => '',
    );

    $form['passwd']['password_confirm'] = array(
            '#type' => 'password',
            '#title' => t('Repeat new password for mailbox - account '.$row['username']),
            '#default_value' => '',
    );

    $form['passwd']['button'] = array(
            '#type' => 'submit',
            '#value' => t('Save'),
    );

    return $form;
} //OK
function mailcontent_settings_passwd_mail_form_validate($form, &$form_state) {
    if ($form_state['values']['password_confirm'] != $form_state['values']['password']) {
        form_set_error('', t('New passwords are not same!!'));
    }
    if ($form_state['values']['password'] == '') {
        form_set_error('', t('Missing new password!!'));
    }
    if ($form_state['values']['password_confirm'] == '') {
        form_set_error('', t('Missing confirmation new password!!'));
    }
    global $user;
    if (db_result(db_query("SELECT pass FROM {mailcontent_login} WHERE duid=".$user->uid." and idacc=".$form_state['values']['idacc']))) {
        $pass = db_query("SELECT pass FROM {mailcontent_login} WHERE duid=".$user->uid." and idacc=".$form_state['values']['idacc']);
        $row = db_fetch_array($pass);
        if ($form_state['values']['password_old'] != $row['pass']) {
            form_set_error('', t('Old password is not correct!'));
        }
    } else {
        form_set_error('', t('Database fault!'));
    }
} //OK
function mailcontent_settings_passwd_mail_form_submit($form, &$form_state) {
    global $user;
    if (user_access('settings accounts')) {


        if (db_result(db_query("SELECT pass FROM {mailcontent_login} WHERE duid=".$user->uid." AND idacc=".$form_state['values']['idacc']))) {
            $pass = db_query("SELECT pass FROM {mailcontent_login} WHERE duid=".$user->uid." AND idacc=".$form_state['values']['idacc']);
            $row = db_fetch_array($pass);
            if ($form_state['values']['password_old'] == $row['pass']) {
                if (db_query("UPDATE {mailcontent_login} SET pass='".$form_state['values']['password']."' WHERE duid=".$user->uid." and idacc=".$form_state['values']['idacc'])) {
                    drupal_set_message(t('Password was changed..'));
                    drupal_goto('mailcontent/settings/accounts');
                }
            } else {
                form_set_error('', t('Bad password!'));
            }
        } else {
            form_set_error('', t('Database fault!'));
        }
    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
} //OK

/*
 * Formulář pro smazání schránky, ze které se budou stahovat maily
 * Implmentace drupal formulářů
 *  - _form -formulář, _form_submit - odeslání formuláře
*/
function mailcontent_settings_del_mail_form($form_state) {
    $form['yes'] = array(
            '#type' => 'submit',
            '#value' => t('Yes'),
    );

    $form['idacc'] = array(
            '#type' => 'hidden',
            '#value' => $GLOBALS['mailcontent_idacc'],
    );

    $form['no'] = array(
            '#type' => 'submit',
            '#value' => t('No'),
            '#submit' => array('mailcontent_goto_settings'),
    );

    return $form;
} //OK
function mailcontent_settings_del_mail_form_submit($form, &$form_state) {
    global $user;
    if (user_access('settings accounts')) {


        if ($form_state['values']['yes'] == 'Yes') {
            global $user;
            if (db_query("DELETE FROM {mailcontent_login} WHERE duid=".$user->uid." and idacc=".$form_state['values']['idacc']) && db_query("DELETE FROM {mailcontent_subscribers} WHERE idacc='".$form_state['values']['idacc']."'")) {
                drupal_set_message(t("Account for mailbox deleted!"));
            } else {
                form_set_error(t("Account for mailbox was not deleted!"));
            }
            drupal_goto('mailcontent/settings/accounts');
        }
    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
} //OK
/* --------------------------- FORMS for operations with mailboxes on WindowsLive END --------------------------------------*/

/* --------------------------- FORMS for operations with allowed mailaddress BEGIN -----------------------------------*/
/*
 * Formulář pro vytvoření mailové adresy, ze které lze posílat maily na web - pro jedenotlivé scráky zvlášť nebo dohromady
 * Implmentace drupal formulářů
 *  - _form -formulář, _form_validate - validace formuláře před odesláním, _form_submit - odeslání formuláře
*/
function mailcontent_settings_add_permision_for_mailaddress_form($form_state) {
    $form['add'] = array(
            '#type' => 'fieldset',
            '#title' => t('Add new mailaddress rule'),
            '#collapsible' => TRUE,
            '#collapsed' => TRUE
    );

    $usernames = mailcontent_get_usernames();

    $form['add']['idacc'] = array(
            '#type' => 'select',
            '#title' => t('Account for rule'),
            '#options' => $usernames
    );

    $form['add']['mailaddress'] = array(
            '#type' => 'textfield',
            '#title' => t('Mailaddress for rule'),
            '#default_value' => ''
    );

    $roles = mailcontent_get_roles();

    $form['add']['role'] = array(
            '#type' => 'select',
            '#title' => t('Role for mailaddress'),
            '#options' => $roles
    );

    $form['add']['button'] = array(
            '#type' => 'submit',
            '#value' => t('Save')
    );

    return $form;
} //OK
function mailcontent_settings_add_permision_for_mailaddress_form_validate($form, &$form_state) {
    if ($form_state['values']['mailaddress'] == '') {
        form_set_error('', t('Missing mailaddress in Add new mailaddress rule!!'));
    }
} //OK
function mailcontent_settings_add_permision_for_mailaddress_form_submit($form, &$form_state) {
    global $user;
    if (user_access('settings accounts')) {


        $write_to_db = mailcontent_db_subscribers_add($form_state['values']['idacc'], $form_state['values']['mailaddress'], $form_state['values']['role']);
        if ($write_to_db) {
            mailcontent_info(t('New rule for mailaddress - '.$form_state["values"]["mailaddress"].' '.t("was created")));
        } else {
            mailcontent_error(t('New rule for mailaddress - ').$form_state["values"]["mailaddress"].' '.t(" was not created"));
        }
    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }

} //OK
/*
 * Formulář pro editaci mailové adresy, ze které lze posílat maily na web - pro jedenotlivé scráky zvlášť nebo dohromady
 * Implmentace drupal formulářů
 *  - _form -formulář, _form_validate - validace formuláře před odesláním, _form_submit - odeslání formuláře
*/
function mailcontent_settings_edit_permision_for_mailaddress_form($form_state) {
    $usernames = mailcontent_get_usernames();
    $roles = mailcontent_get_roles();
    $form['old_mailaddress'] = array(
            '#type' => 'hidden',
            '#title' => t('Mailaddress for rule'),
            '#default_value' => $GLOBALS["mailcontent_mailaddress"]
    );
    $form['old_idacc'] = array(
            '#type' => 'hidden',
            '#title' => t('Account for rule'),
            '#default_value' => $GLOBALS["mailcontent_idacc"]
    );
    $form['old_role'] = array(
            '#type' => 'hidden',
            '#title' => t('Account for rule'),
            '#default_value' => $GLOBALS["mailcontent_role"]
    );


    $form['edit'] = array(
            '#type' => 'fieldset',
            '#title' => t('Edit mailaddress rule'),
            '#collapsible' => FALSE,
            '#collapsed' => FALSE
    );

    $form['edit']['idacc'] = array(
            '#type' => 'select',
            '#title' => t('Account for rule'),
            '#options' => $usernames,
            '#default_value' => $GLOBALS["mailcontent_idacc"]
    );

    $form['edit']['mailaddress'] = array(
            '#type' => 'textfield',
            '#title' => t('Mailaddress for rule'),
            '#default_value' => $GLOBALS["mailcontent_mailaddress"]
    );

    $form['edit']['role'] = array(
            '#type' => 'select',
            '#title' => t('Role for mailaddress'),
            '#options' => $roles,
            '#default_value' => $GLOBALS['mailcontent_role']
    );

    $form['edit']['button'] = array(
            '#type' => 'submit',
            '#value' => t('Save')
    );
    $form['edit']['cancel'] = array(
            '#type' => 'submit',
            '#value' => t('Cancel'),
            '#submit' => array('mailcontent_goto_settings'),
    );
    return $form;
} //OK
function mailcontent_settings_edit_permision_for_mailaddress_form_validate($form, &$form_state) {
    if ($form_state['values']['mailaddress'] == '') {
        form_set_error('', t('Missing mailaddress!'));
    }
} //OK
function mailcontent_settings_edit_permision_for_mailaddress_form_submit($form, &$form_state) {
    global $user;
    if (user_access('settings accounts')) {


        if (db_query("DELETE FROM {mailcontent_subscribers} WHERE duid=".$user->uid." AND mailaddress='".$form_state['values']['old_mailaddress']."' AND idacc='".$form_state['values']['old_idacc']."' AND idr=".$form_state['values']['old_role'])) {
            $write_to_db = mailcontent_db_subscribers_add($form_state['values']['idacc'], $form_state['values']['mailaddress'], $form_state['values']['role']);
            if ($write_to_db) {
                mailcontent_info(t('Rule for mailaddress - '.$form_state["values"]["old_mailaddress"].' '.t("was changed")));
            } else {
                mailcontent_info(t('Rule for mailaddress - ').$form_state["values"]["old_mailaddress"].' '.t(" was only deleted"));
                drupal_goto('mailcontent/settings/mailboxes');
            }
        } else {
            mailcontent_error(t('Unexpected fault with '.$form_state["values"]["old_mailaddress"]));
        }
        drupal_goto('mailcontent/settings/mailboxes');
    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }

} //OK
/*
 * Formulář pro vymazání mailové adresy, ze které lze posílat maily na web - pro jedenotlivé scráky zvlášť nebo dohromady
 * Implmentace drupal formulářů
 *  - _form -formulář, _form_submit - odeslání formuláře
*/
function mailcontent_settings_del_permision_for_mailaddress_form($form_state) {
    $form['yes'] = array(
            '#type' => 'submit',
            '#value' => t('Yes'),
    );

    $form['idacc'] = array(
            '#type' => 'hidden',
            '#value' => $GLOBALS['mailcontent_idacc'],
    );

    $form['mailaddress'] = array(
            '#type' => 'hidden',
            '#value' => $GLOBALS['mailcontent_mailaddress'],
    );

    $form['no'] = array(
            '#type' => 'submit',
            '#value' => t('No'),
            '#submit' => array('mailcontent_goto_settings'),
    );

    return $form;
} //OK
function mailcontent_settings_del_permision_for_mailaddress_form_submit($form, &$form_state) {
    global $user;
    if (user_access('settings accounts')) {


        if ($form_state['values']['yes'] == 'Yes') {
            global $user;
            if (db_query("DELETE FROM {mailcontent_subscribers} WHERE duid=".$user->uid." and mailaddress='".$form_state['values']['mailaddress']."' and idacc='".$form_state['values']['idacc']."'")) {
                mailcontent_info(t("Mailaddress"." ".$form_state['values']['mailaddress']." ".t('for')." ".$form_state['values']['username']." ".t('was successful deleted!')));
            } else {
                mailcontent_error(t("Mailaddress"." ".$form_state['values']['mailaddress']." ".t('for')." ".mailcontent_get_username($form_state['values']['idacc'])." ".t('was not deleted!')));
            }
            drupal_goto('mailcontent/settings/mailboxes');
        }
    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }

} //OK
/* --------------------------- FORMS for operations with allowed mailaddress END --------------------------------------*/

/* --------------------------- FORMS for Actions BEGIN -----------------------------------*/
/*
 * Formulář pro vytvoření akce systému
 * Implmentace drupal formulářů
 *  - _form -formulář, _form_validate - validace formuláře před odesláním, _form_submit - odeslání formuláře
*/
function mailcontent_settings_add_action_form($form_state) {
    $form['add'] = array(
            '#type' => 'fieldset',
            '#title' => t('Add new action'),
            '#collapsible' => TRUE,
            '#collapsed' => TRUE,
    );
    $form['add']['action'] = array(
            '#type' => 'textfield',
            '#title' => t('Name of action'),
            '#default_value' => ''
    );

    $form['add']['description'] = array(
            '#type' => 'textfield',
            '#title' => t('Description of action'),
            '#default_value' => '');

    $form['add']['path'] = array(
            '#type' => 'textfield',
            '#title' => t('Path for callback - only for advanced users'),
            '#default_value' => '');

    $form['add']['button'] = array(
            '#type' => 'submit',
            '#value' => t('Save'));

    return $form;
} //OK
function mailcontent_settings_add_action_form_validate($form, &$form_state) {
    if ($form_state['values']['action'] == '') {
        form_set_error('', t('Missing action in Add new action!!'));
    }


} //OK
function mailcontent_settings_add_action_form_submit($form, &$form_state) {
    global $user;
    if (user_access('settings actions')) {


        $write_to_db = mailcontent_db_action_add($form_state['values']['action'], $form_state['values']['description'], $form_state['values']['path']);
        if ($write_to_db) {
            drupal_set_message(t('New action created..'));
            $ida = db_last_insert_id("mailcontent_actions", "ida");
            if (db_result(db_query("SELECT idr FROM {mailcontent_role} WHERE name = 'admin'"))) {
                $result = db_query("SELECT idr FROM {mailcontent_role} WHERE name = 'admin'");
                while($row = db_fetch_array($result)) {
                    $data['idr'] = $row['idr'];
                    $data['ida'] = $ida;
                    if (mailcontent_db_add_role_actions($data)) {
                        mailcontent_info(t('Action is allowed for admin..'));
                    }
                }
                return true;
            }
            mailcontent_info(t('New action created, but not added as allowed for admin!'));
        } else {
            form_set_error(t('Fault - new action was not created!'));
            return false;
        }

    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }

}//Ok
/*
 * Formulář pro editaci akce systému
 * Implmentace drupal formulářů
 *  - _form -formulář, _form_validate - validace formuláře před odesláním, _form_submit - odeslání formuláře
*/
function mailcontent_settings_edit_action_form($form_state) {
    global $user;
    if (db_result(db_query("SELECT * FROM {mailcontent_actions} WHERE ida=".$GLOBALS['mailcontent_ida']))) {
        $result = db_query("SELECT * FROM {mailcontent_actions} WHERE ida=".$GLOBALS['mailcontent_ida']);
        $row = db_fetch_array($result);
    } else {
        mailcontent_error(t('Fault!! - can not load data from database - actions'));
        drupal_goto('mailcontent/settings');
    }
    $form['ida'] = array(
            '#type' => 'hidden',
            '#title' => t('ida of action to edit'),
            '#default_value' => $row['ida'],
    );
    $form['edit'] = array(
            '#type' => 'fieldset',
            '#title' => t('Change values for '.$row['name']),
            '#collapsible' => TRUE,
            '#collapsed' => FALSE,
    );
    $form['edit']['action'] = array(
            '#type' => 'textfield',
            '#title' => t('Name of action'),
            '#default_value' => $row['name'],
    );
    $form['edit']['description'] = array(
            '#type' => 'textfield',
            '#title' => t('Description for action'),
            '#default_value' => $row['description']
    );
    $form['edit']['path'] = array(
            '#type' => 'textfield',
            '#title' => t('Path for callback - only for advanced users'),
            '#default_value' => $row['path']
    );
    $form['edit']['button'] = array(
            '#type' => 'submit',
            '#value' => t('Save'),
    );
    $form['edit']['cancel'] = array(
            '#type' => 'submit',
            '#value' => t('Cancel'),
            '#submit' => array('mailcontent_goto_settings'),
    );
    return $form;
} //OK
function mailcontent_settings_edit_action_form_validate($form, &$form_state) {
    if ($form_state['values']['action'] == '') {
        form_set_error('', t('Name of action is missing!!'));
    }
} //OK
function mailcontent_settings_edit_action_form_submit($form, &$form_state) {
    global $user;
    if (user_access('settings actions')) {


        global $user;
        if (db_query("UPDATE {mailcontent_actions} SET name='".$form_state['values']['action']."', description='".$form_state['values']['description']."', path='".$form_state['values']['path']."' WHERE ida=".$form_state['values']['ida'])) {
            drupal_set_message(t('Action has been changed..'));
            drupal_goto('mailcontent/settings/actions');
        } else {
            form_set_error('', t('Database fault'));
        }

    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }

} //OK
/*
 * Formulář pro vymazání akce systému
 * Implmentace drupal formulářů
 *  - _form -formulář, _form_submit - odeslání formuláře
*/
function mailcontent_settings_del_action_form($form_state) {
    $form['yes'] = array(
            '#type' => 'submit',
            '#value' => t('Yes'),
    );

    $form['ida'] = array(
            '#type' => 'hidden',
            '#value' => $GLOBALS['mailcontent_ida'],
    );

    $form['no'] = array(
            '#type' => 'submit',
            '#value' => t('No'),
            '#submit' => array('mailcontent_goto_settings')
    );

    return $form;
} //OK
function mailcontent_settings_del_action_form_submit($form, &$form_state) {
    global $user;
    if (user_access('settings actions')) {


        if ($form_state['values']['yes'] == 'Yes') {
            global $user;
            $res = db_query("SELECT name FROM {mailcontent_actions} WHERE ida=".$form_state['values']['ida']);
            $rw = db_fetch_array($res);
            if (db_query("DELETE FROM {mailcontent_actions} WHERE ida=".$form_state['values']['ida']) && db_query("DELETE FROM {mailcontent_actions_keywords} WHERE ida=".$form_state['values']['ida']) && db_query("DELETE FROM {mailcontent_role_actions} WHERE ida=".$form_state['values']['ida'])) {
                drupal_set_message(t('Action')." ".$rw['name']." ".t('deleted!'));
            } else {
                form_set_error(t('Database error'));
            }
            drupal_goto('mailcontent/settings/actions');
        }

    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }


} //OK



/*
 * Formulář pro vytvoření klíčového slova systému
 * Implmentace drupal formulářů
 *  - _form -formulář, _form_validate - validace formuláře před odesláním, _form_submit - odeslání formuláře
*/
function mailcontent_settings_add_keyword_form($form_state) {
    $form['add'] = array(
            '#type' => 'fieldset',
            '#title' => t('Add new keyword'),
            '#collapsible' => TRUE,
            '#collapsed' => TRUE
    );

    $form['add']['keyword'] = array(
            '#type' => 'textfield',
            '#title' => t('Keyword'),
            '#default_value' => ''
    );

    $form['add']['class'] = array(
            '#type' => 'textfield',
            '#title' => t('Class'),
            '#default_value' => ''
    );

    $form['add']['description'] = array(
            '#type' => 'textfield',
            '#title' => t('Description for keyword'),
            '#default_value' => ''
    );

    $actions = mailcontent_get_actions();

    $form['add']['add_act'] = array(
            '#type' => 'fieldset',
            '#title' => t('Allowed actions'),
            '#collapsible' => FALSE
    );
    foreach($actions as $name => $value) {
        $form['add']['add_act'][$name] = array(
                '#type' => 'checkbox',
                '#title' => $value,
        );
    }

    $form['add']['button'] = array(
            '#type' => 'submit',
            '#value' => t('Save')
    );

    return $form;
}
function mailcontent_settings_add_keyword_form_validate($form, &$form_state) {
    if ($form_state['values']['keyword'] == '') {
        form_set_error('', t('Missing keyword in Add new keyword!!'));
    }
}
function mailcontent_settings_add_keyword_form_submit($form, &$form_state) {

    global $user;
    if (user_access('settings actions')) {
        if ($form_state['values']['class'] == '') {
            $class = 'other';
        } else {
            $class = $form_state['values']['class'];
        }
        $data = array(
                'keyword' => $form_state['values']['keyword'],
                'class' => $class,
                'description' => $form_state['values']['description']
        );

        if (drupal_write_record('mailcontent_keywords', $data)) {
            $idw = db_last_insert_id('mailcontent_keywords', 'idw');
            mailcontent_info(t('Keyword '.$form_state['values']['keyword'].' was added'));
            $actions = mailcontent_get_actions();
            foreach($actions as $name => $value) {
                drupal_set_message("jmeno - ".$name." - ".$form_state['values'][$name]);
                if ($form_state['values'][$name] == 1) {
                    $data = array(
                            'ida' => $name,
                            'idw' => $idw
                    );
                    if (!drupal_write_record('mailcontent_actions_keywords', $data)) {
                        mailcontent_error(t('Actions for keyword '.$form_state['values']['keyword'].' was not added'));
                        drupal_goto('mailcontent/settings');
                    }
                }
            }
            mailcontent_info(t('Actions for keyword was added'));
        } else {
            mailcontent_error(t('Keyword '.$form_state['values']['keyword'].' was not added'));
        }

    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
}
/*
 * Formulář pro editaci klíčového slova systému
 * Implmentace drupal formulářů
 *  - _form -formulář, _form_validate - validace formuláře před odesláním, _form_submit - odeslání formuláře
*/
function mailcontent_settings_edit_keyword_form($form_state) {
    if (db_result(db_query("SELECT * FROM {mailcontent_keywords} WHERE idw=".$GLOBALS['mailcontent_idw']))) {
        $result = db_query("SELECT * FROM {mailcontent_keywords} WHERE idw=".$GLOBALS['mailcontent_idw']);
        $row = db_fetch_array($result);
    } else {
        drupal_set_message(t('Fault!! - can not load data from database', 'warning'));
        drupal_goto('mailcontent/settings');
    }

    $form['idw'] = array(
            '#type' => 'hidden',
            '#value' => $GLOBALS['mailcontent_idw'],
            '#title' => t('idw for keyword to edit')
    );

    $form['edit'] = array(
            '#type' => 'fieldset',
            '#title' => t('Edit keyword'),
            '#collapsible' => TRUE,
            '#collapsed' => FALSE
    );

    $form['edit']['keyword'] = array(
            '#type' => 'textfield',
            '#title' => t('Keyword'),
            '#default_value' => $row['keyword']
    );

    $form['edit']['class'] = array(
            '#type' => 'textfield',
            '#title' => t('Class'),
            '#default_value' => $row['class']
    );

    $form['edit']['description'] = array(
            '#type' => 'textfield',
            '#title' => t('Description for keyword'),
            '#default_value' => $row['description']
    );

    if (db_result(db_query("SELECT * FROM {mailcontent_actions_keywords} WHERE idw=".$GLOBALS['mailcontent_idw']))) {
        $result = db_query("SELECT * FROM {mailcontent_actions_keywords} WHERE idw=".$GLOBALS['mailcontent_idw']);
        while ($row = db_fetch_array($result)) {
            $tofarr[$row['ida']] = $row['ida'];
        }
    }

    $actions = mailcontent_get_actions();
// TODO dodelat selekci vsech najednou a deselkci vsech..
    $form['edit']['add_act'] = array(
            '#type' => 'fieldset',
            '#title' => t('Allowed actions'),
            '#collapsible' => FALSE
    );

    foreach($actions as $name => $value) {
        if (isset($tofarr[$name])) {
            $tof = 1;
        } else {
            $tof = 0;
        }
        $form['edit']['add_act'][$name] = array(
                '#type' => 'checkbox',
                '#title' => $value,
                '#default_value' => $tof
        );
    }

    $form['edit']['button'] = array(
            '#type' => 'submit',
            '#value' => t('Save')
    );

    $form['edit']['button2'] = array(
            '#type' => 'submit',
            '#value' => t('Cancel'),
            '#submit' => array('mailcontent_goto_settings')
    );
    return $form;
} //OK
function mailcontent_settings_edit_keyword_form_validate($form, &$form_state) {
    if ($form_state['values']['keyword'] == '') {
        form_set_error('', t('Missing keyword!'));
    }
} //OK
function mailcontent_settings_edit_keyword_form_submit($form, &$form_state) {
    global $user;
    if (user_access('settings actions')) {
        if ($form_state['values']['class'] == '') {
            $class = 'other';
        } else {
            $class = $form_state['values']['class'];
        }

        $data = array(
                'keyword' => $form_state['values']['keyword'],
                'class' => $class,
                'description' => $form_state['values']['description']
        );

        if (db_query("UPDATE {mailcontent_keywords} SET keyword='".$form_state['values']['keyword']."', description='".$form_state['values']['description']."', class='".$class."' WHERE idw=".$form_state['values']['idw'])) {
            mailcontent_info(t('Keyword '.$form_state['values']['keyword'].' was edited.'));
            if (db_result(db_query("SELECT * FROM {mailcontent_actions_keywords} WHERE idw=".$form_state['values']['idw']))) {
                if(db_query("DELETE FROM {mailcontent_actions_keywords} WHERE idw=".$form_state['values']['idw'])) {
                } else {
                    mailcontent_error(t('Actions for keyword '.$form_state['values']['keyword'].' can not be modified'));
                    drupal_goto('mailcontent/settings/keywords');
                }
            }
            $actions = mailcontent_get_actions();
            foreach($actions as $name => $value) {

                if ($form_state['values'][$name] == 1) {
                    $data = array(
                            'ida' => $name,
                            'idw' => $form_state['values']['idw']
                    );
                    if (!drupal_write_record('mailcontent_actions_keywords', $data)) {
                        mailcontent_error(t('Actions for keyword '.$form_state['values']['keyword'].' was not edited'));
                        drupal_goto('mailcontent/settings/keywords');
                    }
                }
            }
            mailcontent_info(t('Actions for keyword '.$form_state['values']['keyword'].' was edited'));
            drupal_goto('mailcontent/settings/keywords');
        }

    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
} //OK
/*
 * Formulář pro vymazání klíčového slova systému
 * Implmentace drupal formulářů
 *  - _form -formulář, _form_validate - validace formuláře před odesláním, _form_submit - odeslání formuláře
*/
function mailcontent_settings_del_keyword_form($form_state) {
    $form['yes'] = array(
            '#type' => 'submit',
            '#value' => t('Yes'),
    );

    $form['idw'] = array(
            '#type' => 'hidden',
            '#value' => $GLOBALS['mailcontent_idw'],
    );

    $form['no'] = array(
            '#type' => 'submit',
            '#value' => t('No'),
            '#submit' => array('mailcontent_goto_settings'),
    );

    return $form;
} //OK
function mailcontent_settings_del_keyword_form_submit($form, &$form_state) {
    global $user;
    if (user_access('settings actions')) {
        if ($form_state['values']['yes'] == 'Yes') {
            global $user;
            if (db_query("DELETE FROM {mailcontent_keywords} WHERE idw=".$form_state['values']['idw']) && db_query("DELETE FROM {mailcontent_actions_keywords} WHERE idw=".$form_state['values']['idw'])) {
                mailcontent_info(t("Keyword was successful deleted!"));
            } else {
                mailcontent_error(t("Keyword was not deleted!"), 'error');
            }
            drupal_goto('mailcontent/settings/keywords');
        }

    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
} //OK
/* --------------------------- FORMS for keywords END --------------------------------------*/


/*
 * Formulář pro vytvoření role
 * Implmentace drupal formulářů
 *  - _form -formulář, _form_validate - validace formuláře před odesláním, _form_submit - odeslání formuláře
*/
function mailcontent_settings_add_role_form($form_state) {
    $form['add'] = array(
            '#type' => 'fieldset',
            '#title' => t('Add new role'),
            '#collapsible' => TRUE,
            '#collapsed' => TRUE
    );

    $form['add']['name'] = array(
            '#type' => 'textfield',
            '#title' => t('Name of role'),
            '#default_value' => ''
    );

    $form['add']['description'] = array(
            '#type' => 'textfield',
            '#title' => t('Description for role'),
            '#default_value' => ''
    );

    $actions = mailcontent_get_actions();
// TODO dodelat selekci vsech najednou a deselkci vsech..
    $form['add']['add_act'] = array(
            '#type' => 'fieldset',
            '#title' => t('Allowed actions'),
            '#collapsible' => FALSE
    );
    foreach($actions as $name => $value) {
        $form['add']['add_act'][$name] = array(
                '#type' => 'checkbox',
                '#title' => $value,
        );
    }

    $form['add']['button'] = array(
            '#type' => 'submit',
            '#value' => t('Save')
    );

    return $form;
} //OK
function mailcontent_settings_add_role_form_validate($form, &$form_state) {
    if ($form_state['values']['name'] == '') {
        form_set_error('', t('Missing name of role in Add new role!!'));
    }
} //OK
function mailcontent_settings_add_role_form_submit($form, &$form_state) {
    global $user;
    if (user_access('settings roles')) {
        $data = array(
                'name' => $form_state['values']['name'],
                'description' => $form_state['values']['description']
        );

        if (drupal_write_record('mailcontent_role', $data)) {
            $idr = db_last_insert_id('mailcontent_role', 'idr');
            mailcontent_info(t('Role '.$form_state['values']['name'].' was added'));
            $actions = mailcontent_get_actions();
            foreach($actions as $name => $value) {
                if ($form_state['values'][$name] == 1) {
                    $data = array(
                            'ida' => $name,
                            'idr' => $idr
                    );
                    if (!drupal_write_record('mailcontent_role_actions', $data)) {
                        mailcontent_error(t('Allowed actions for role '.$form_state['values']['name'].' was not added'));
                        drupal_goto('mailcontent/settings');
                    }
                }
            }
            mailcontent_info(t('Allowed actions for role '.$form_state['values']['name'].' was added'));
        } else {
            mailcontent_error(t('Role '.$form_state['values']['name'].' was not added'));
        }
    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }


} //OK

/*
 * Formulář pro editaci role
 * Implmentace drupal formulářů
 *  - _form -formulář, _form_validate - validace formuláře před odesláním, _form_submit - odeslání formuláře
*/
function mailcontent_settings_edit_role_form($form_state) {
    if (db_result(db_query("SELECT * FROM {mailcontent_role} WHERE idr=".$GLOBALS['mailcontent_idr']))) {
        $result = db_query("SELECT * FROM {mailcontent_role} WHERE idr=".$GLOBALS['mailcontent_idr']);
        $row = db_fetch_array($result);
    } else {
        drupal_set_message(t('Fault!! - can not load data from database', 'warning'));
        drupal_goto('mailcontent/settings');
    }

    $form['idr'] = array(
            '#type' => 'hidden',
            '#value' => $row['idr'],
            '#title' => t('idr for role to edit')
    );

    $form['edit'] = array(
            '#type' => 'fieldset',
            '#title' => t('Edit role'),
            '#collapsible' => TRUE,
            '#collapsed' => FALSE
    );

    $form['edit']['name'] = array(
            '#type' => 'textfield',
            '#title' => t('Name of role'),
            '#default_value' => $row['name']
    );

    $form['edit']['description'] = array(
            '#type' => 'textfield',
            '#title' => t('Description for role'),
            '#default_value' => $row['description']
    );

    if (db_result(db_query("SELECT * FROM {mailcontent_role_actions} WHERE idr=".$GLOBALS['mailcontent_idr']))) {
        $result = db_query("SELECT * FROM {mailcontent_role_actions} WHERE idr=".$GLOBALS['mailcontent_idr']);
        while ($row = db_fetch_array($result)) {
            $tofarr[$row['ida']] = $row['ida'];
        }
    }

    $actions = mailcontent_get_actions();
// TODO dodelat selekci vsech najednou a deselkci vsech..
    $form['edit']['add_act'] = array(
            '#type' => 'fieldset',
            '#title' => t('Allowed actions'),
            '#collapsible' => FALSE
    );

    foreach($actions as $name => $value) {
        if (isset($tofarr[$name])) {
            $tof = 1;
        } else {
            $tof = 0;
        }
        $form['edit']['add_act'][$name] = array(
                '#type' => 'checkbox',
                '#title' => $value,
                '#default_value' => $tof
        );
    }

    $form['edit']['button'] = array(
            '#type' => 'submit',
            '#value' => t('Save')
    );

    $form['edit']['button2'] = array(
            '#type' => 'submit',
            '#value' => t('Cancel'),
            '#submit' => array('mailcontent_goto_settings')
    );
    return $form;
} //OK
function mailcontent_settings_edit_role_form_validate($form, &$form_state) {
    if ($form_state['values']['name'] == '') {
        form_set_error('', t('Missing name!'));
    }
}//OK
function mailcontent_settings_edit_role_form_submit($form, &$form_state) {
    global $user;
    if (user_access('settings roles')) {
        $data = array(
                'name' => $form_state['values']['name'],
                'description' => $form_state['values']['description']
        );

        if (db_query("UPDATE {mailcontent_role} SET name='".$form_state['values']['name']."', description='".$form_state['values']['description']."' WHERE idr=".$form_state['values']['idr'])) {
            mailcontent_info(t('Role '.$form_state['values']['name'].' was edited.'));
            if (db_result(db_query("SELECT * FROM {mailcontent_role_actions} WHERE idr=".$form_state['values']['idr']))) {
                if(db_query("DELETE FROM {mailcontent_role_actions} WHERE idr=".$form_state['values']['idr'])) {
                } else {
                    mailcontent_error(t('Allowed actions for role '.$form_state['values']['name'].' can not be modified'));
                    drupal_goto('mailcontent/settings/roles');
                }
            }
            $actions = mailcontent_get_actions();
            foreach($actions as $name => $value) {
                if ($form_state['values'][$name] == 1) {
                    $data = array(
                            'ida' => $name,
                            'idr' => $form_state['values']['idr']
                    );
                    if (!drupal_write_record('mailcontent_role_actions', $data)) {
                        mailcontent_error(t('Allowed actions for role '.$form_state['values']['name'].' was not edited'));
                        drupal_goto('mailcontent/settings/roles');
                    }
                }
            }
            mailcontent_info(t('Allowed actions for role '.$form_state['values']['name'].' was edited'));
            drupal_goto('mailcontent/settings/roles');
        }
    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }


} //OK

/*
 * Formulář smazání role
 * Implmentace drupal formulářů
 *  - _form -formulář, _form_validate - validace formuláře před odesláním, _form_submit - odeslání formuláře
*/
function mailcontent_settings_del_role_form($form_state) {
    $form['yes'] = array(
            '#type' => 'submit',
            '#value' => t('Yes'),
    );

    $form['idr'] = array(
            '#type' => 'hidden',
            '#value' => $GLOBALS['mailcontent_idr'],
    );

    $form['no'] = array(
            '#type' => 'submit',
            '#value' => t('No'),
            '#submit' => array('mailcontent_goto_settings'),
    );

    return $form;
} //OK
function mailcontent_settings_del_role_form_submit($form, &$form_state) {
    global $user;
    if (user_access('settings roles')) {
        if ($form_state['values']['yes'] == 'Yes') {
            global $user;
            if (db_query("DELETE FROM {mailcontent_role} WHERE idr=".$form_state['values']['idr']) && db_query("DELETE FROM {mailcontent_role_actions} WHERE idr=".$form_state['values']['idr'])) {
                mailcontent_info(t("Role was successful deleted!"));
            } else {
                mailcontent_error(t("Role was not deleted!"));
            }
            drupal_goto('mailcontent/settings/roles');
        }
    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }


} //OK




/*
 * Formulář smazání mailů
 * Implmentace drupal formulářů
 *  - _form -formulář, _form_validate - validace formuláře před odesláním, _form_submit - odeslání formuláře
*/
function mailcontent_del_mail_form($form_state) {
    $form['yes'] = array(
            '#type' => 'submit',
            '#value' => t('Yes')
    );

    $form['idm'] = array(
            '#type' => 'hidden',
            '#value' => $GLOBALS['mailcontent_idm']
    );

    $form['return'] = array(
            '#type' => 'hidden',
            '#value' => $GLOBALS['mailcontent_ret']
    );

    $form['no'] = array(
            '#type' => 'submit',
            '#value' => t('No'),
            '#submit' => array('mailcontent_goto_content')
    );

    return $form;
} //OK
function mailcontent_del_mail_form_submit($form, &$form_state) {
    global $user;
    if (user_access('content edit')) {


        if ($form_state['values']['yes'] == 'Yes') {
            global $user;
            if (db_query("DELETE FROM {mailcontent_mail} WHERE idm=".$form_state['values']['idm'])) {
                mailcontent_info(t("Mail was successful deleted!"));
            } else {
                mailcontent_error(t("Mail was not deleted!"));
            }
            if ($form_state['values']['return'] != '') {
                drupal_goto('mailcontent/content/'.$form_state['values']['return']."/all");
            } else {
                drupal_goto('mailcontent/content');
            }
        }
    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
} //OK


/*
 * Formulář pro přidání pokorčílého nastavení
 * Implmentace drupal formulářů
 *  - _form -formulář, _form_validate - validace formuláře před odesláním, _form_submit - odeslání formuláře
*/
function mailcontent_settings_add_other_form($form_state) {
    $form['add'] = array(
            '#type' => 'fieldset',
            '#title' => t('Add value to service table'),
            '#collapsible' => TRUE,
            '#collapsed' => TRUE
    );

    $form['add']['name'] = array(
            '#type' => 'textfield',
            '#title' => t('Name of service value'),
            '#default_value' => ''
    );

    $form['add']['value'] = array(
            '#type' => 'textfield',
            '#title' => t('Value for service'),
            '#default_value' => ''
    );

    $form['add']['description'] = array(
            '#type' => 'textfield',
            '#title' => t('Description for service value'),
            '#default_value' => ''
    );

    $form['add']['button'] = array(
            '#type' => 'submit',
            '#value' => t('Save')
    );

    return $form;
}//OK
function mailcontent_settings_add_other_form_validate($form, &$form_state) {
    if ($form_state['values']['name'] == '') {
        form_set_error('', t('Missing name of service value'));
    }
}//OK
function mailcontent_settings_add_other_form_submit($form, &$form_state) {


    global $user;
    if (user_access('settings actions')) {


        $data = array(
                'name' => $form_state['values']['name'],
                'description' => $form_state['values']['description'],
                'value' => $form_state['values']['value']
        );

        if (drupal_write_record('mailcontent_servis', $data)) {
            mailcontent_info(t('Service value was added'));
        } else {
            mailcontent_error(t('Service value was not added'));

        }

    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }


}//OK

/*
 * Formulář pro přidání pokorčílého nastavení
 * Implmentace drupal formulářů
 *  - _form -formulář, _form_validate - validace formuláře před odesláním, _form_submit - odeslání formuláře
*/
function mailcontent_settings_edit_other_form($form_state) {
    if (db_result(db_query("SELECT * FROM {mailcontent_servis} WHERE id=".$GLOBALS['mailcontent_id']))) {
        $result = db_query("SELECT * FROM {mailcontent_servis} WHERE id=".$GLOBALS['mailcontent_id']);
        $row = db_fetch_array($result);
    } else {
        drupal_set_message(t('Fault!! - can not load data from database', 'warning'));
        drupal_goto('mailcontent/settings');
    }

    $form['ids'] = array(
            '#type' => 'hidden',
            '#value' => $GLOBALS['mailcontent_id'],
            '#title' => t('id for service value to edit')
    );

    $form['edit'] = array(
            '#type' => 'fieldset',
            '#title' => t('Edit service value'),
            '#collapsible' => TRUE,
            '#collapsed' => FALSE
    );

    $form['edit']['name'] = array(
            '#type' => 'textfield',
            '#title' => t('Name of service value'),
            '#default_value' => $row['name']
    );

    $form['edit']['value'] = array(
            '#type' => 'textfield',
            '#title' => t('Value for service value'),
            '#default_value' => $row['value']
    );

    $form['edit']['description'] = array(
            '#type' => 'textfield',
            '#title' => t('Description for service value'),
            '#default_value' => $row['description']
    );

    $form['edit']['button'] = array(
            '#type' => 'submit',
            '#value' => t('Save')
    );

    $form['edit']['button2'] = array(
            '#type' => 'submit',
            '#value' => t('Cancel'),
            '#submit' => array('mailcontent_goto_settings')
    );
    return $form;
}//OK
function mailcontent_settings_edit_other_form_validate($form, &$form_state) {
    if ($form_state['values']['name'] == '') {
        form_set_error('', t('Missing name!'));
    }
}//OK
function mailcontent_settings_edit_other_form_submit($form, &$form_state) {
    global $user;
    if (user_access('settings actions')) {


        $data = array(
                'name' => $form_state['values']['name'],
                'value' => $form_state['values']['value'],
                'description' => $form_state['values']['description']
        );

        if (db_query("UPDATE {mailcontent_servis} SET name='".$form_state['values']['name']."', description='".$form_state['values']['description']."', value='".$form_state['values']['value']."' WHERE id=".$form_state['values']['ids'])) {
            mailcontent_info(t('Service value was edited.'));
            drupal_goto('mailcontent/settings/others');
        } else {
            mailcontent_error(t('Service value was not edited.'));
            drupal_goto('mailcontent/settings/others');

        }

    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }


}//OK
/*
 * Formulář pro přidání pokorčílého nastavení
 * Implmentace drupal formulářů
 *  - _form -formulář, _form_validate - validace formuláře před odesláním, _form_submit - odeslání formuláře
*/
function mailcontent_settings_del_other_form($form_state) {
    $form['yes'] = array(
            '#type' => 'submit',
            '#value' => t('Yes'),
    );

    $form['id'] = array(
            '#type' => 'hidden',
            '#value' => $GLOBALS['mailcontent_id'],
    );

    $form['no'] = array(
            '#type' => 'submit',
            '#value' => t('No'),
            '#submit' => array('mailcontent_goto_settings'),
    );

    return $form;
}//OK
function mailcontent_settings_del_other_form_submit($form, &$form_state) {
    global $user;
    if (user_access('settings actions')) {


        if ($form_state['values']['yes'] == 'Yes') {
            global $user;
            if (db_query("DELETE FROM {mailcontent_servis} WHERE id=".$form_state['values']['id'])) {
                mailcontent_info(t("Service value was successful deleted!"));
            } else {
                mailcontent_error(t("Serivce value was not deleted!"));
            }
        }
        drupal_goto('mailcontent/settings/others');

    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }


}//OK
/*
 * Formulář pro vykreslení ankety
 * Implmentace drupal formulářů
 *  - _form -formulář, _form_validate - validace formuláře před odesláním, _form_submit - odeslání formuláře
*/
function mailcontent_iquiry_form($form_state) {
    global $user;
    $result = db_query("SELECT * FROM {mailcontent_content} WHERE idm=".$GLOBALS['mailcontent_idm']." AND type_content='inquiry_question'");
    if ($result) {
        $form['inq'] = array(
                '#type' => 'fieldset',
                '#title' => t('Select your choice'),
                '#collapsible' => FALSE,
                '#collapsed' => FALSE
        );

        $form['inq']['idm'] = array(
                '#type' => 'hidden',
                '#title' => t('ID of mail'),
                '#value' => $GLOBALS['mailcontent_idm']
        );

        $i = 0;
        while($row = db_fetch_array($result)) {
            if ($i == 0) {
                $i = 1;
                $first = $row['idc'];
            }
            $votes = 0;
            $res = db_query("SELECT COUNT(vote_duid) FROM {mailcontent_inquries_votes} WHERE vote_idm=".$GLOBALS['mailcontent_idm']." AND vote_idc=".$row['idc']);
            if ($res) {
                $rw = db_fetch_array($res);
                foreach ($rw as $val) {
                    $votes = $val;
                }
            }
            $questions[$row['idc']] = $row['value_content']."&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<b>( ".$votes.t(" votes").")</b>";
        }

        $result = db_query("SELECT * FROM {mailcontent_inquries_votes} WHERE vote_idm=".$GLOBALS['mailcontent_idm']." AND vote_duid=".$user->uid);
        if ($result) {
            while($row = db_fetch_array($result)) {
                $first = $row['vote_idc'];
            }
        }

        $form['inq']['idc'] = array(
                '#type' => 'radios',
                '#title' => mailcontent_get_title($GLOBALS['mailcontent_idm']),
                '#options' => $questions,
                '#default_value' => variable_get('idc', $first)
        );

        $form['inq']['button'] = array(
                '#type' => 'submit',
                '#value' => t('Save')
        );
    } else {
        drupal_goto("mailcontent/content/inquiry/all");
        return false;
    }
    return $form;
} //OK
function mailcontent_iquiry_form_submit($form, &$form_state) {

    global $user;
    if (user_access('content user')) {
        if (db_result(db_query("SELECT * FROM {mailcontent_inquries_votes} WHERE vote_idm=".$form_state['values']['idm']." AND vote_duid=".$user->uid))) {
            if (db_query("UPDATE {mailcontent_inquries_votes} SET vote_idc=".$form_state['values']['idc']." WHERE vote_idm=".$form_state['values']['idm']." AND vote_duid=".$user->uid)) {
                mailcontent_info(t("Your vote has been changed."));
                drupal_goto("mailcontent/content/inquiry/all");
                return true;
            } else {
                mailcontent_error(t("Your vote can not be saved."));
                drupal_goto("mailcontent/content/inquiry/all");
                return false;
            }
        } else {
            $data = array(
                    'vote_idm' => $form_state['values']['idm'],
                    'vote_idc' => $form_state['values']['idc'],
                    'vote_duid' =>  $user->uid
            );
            if (drupal_write_record('mailcontent_inquries_votes', $data)) {
                mailcontent_info(t("Your vote has been saved."));
                drupal_goto("mailcontent/content/inquiry/all");
                return true;
            } else {
                mailcontent_error(t("Your vote can not be saved."));
                drupal_goto("mailcontent/content/inquiry/all");
                return false;
            }
        }

    } else {
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }


}//OK



?>
